package com.kangfu.api.service.impl;

import com.kangfu.api.service.IMessageService;
import com.kangfu.common.exception.BusinessException;
import com.kangfu.db.domain.entity.Message;
import com.kangfu.db.domain.entity.SysUser;
import com.kangfu.db.domain.entity.User;
import com.kangfu.db.mapper.MessageMapper;
import com.kangfu.db.mapper.SysUserMapper;
import com.kangfu.db.mapper.UserMapper;
import com.kangfu.db.model.request.MessageRequest;
import com.kangfu.db.model.vo.MessageContentVo;
import com.kangfu.db.model.vo.MessageInfoVo;
import com.kangfu.db.model.vo.MessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@Service
public class MessageServiceImpl implements IMessageService {

    @Autowired
    public MessageMapper messageMapper;
    @Autowired
    public UserMapper userMapper;
    @Autowired
    public SysUserMapper sysUserMapper;

    @Override
    public List<MessageVo> getMessageList(int userId, int page) {
        if (page <= 1) {
            page = 1;
        }
        int pageSize = 5;
        int offset = (page - 1)*pageSize;
        List<Integer> idList = messageMapper.selectIdByUser(userId, offset, pageSize);
        if (idList == null || idList.isEmpty()) {
            return null;
        }
        return messageMapper.selectMessageList(idList);
    }

    @Override
    public MessageInfoVo getMessageInfo(int userId, int doctorId, int page, int lastId) {
        if (page <= 1) {
            page = 1;
        }
        int pageSize = 20;
        int offset = (page - 1)*pageSize;
        User user = userMapper.selectUserById(userId);
        SysUser sysUser = sysUserMapper.selectUserById(doctorId);
        if (sysUser == null) {
            throw new BusinessException("不存在的客服");
        }
        List<MessageContentVo> contentList = messageMapper.selectContentList(userId, doctorId, offset, pageSize, lastId);
        Collections.reverse(contentList);
        MessageInfoVo messageInfoVo = new MessageInfoVo();
        messageInfoVo.setDoctorAvatar(sysUser.getAvatar());
        messageInfoVo.setDoctorName(sysUser.getNickName());
        messageInfoVo.setAvatar(user.getAvatar());
        messageInfoVo.setContentList(contentList);
        if (lastId == 0) {
            if (contentList == null || contentList.isEmpty()) {
                messageInfoVo.setLastId(lastId);
            } else {
                messageInfoVo.setLastId(contentList.get(0).getId());
            }
        } else {
            messageInfoVo.setLastId(lastId);
        }

        //每次只要一请求，就会把所有的未读消息都变成已读
        messageMapper.updateReadFlag(userId, doctorId);

        return messageInfoVo;
    }

    @Override
    public void addMessage(int userId, MessageRequest messageRequest) {
        Message message = new Message();
        message.setUserId(userId);
        message.setSysUserId(messageRequest.getDoctorId());
        message.setType(messageRequest.getType());
        message.setContent(messageRequest.getContent());
        message.setUserReadFlag(1);
        message.setSendTime(LocalDateTime.now());
        message.setCreateTime(LocalDateTime.now());
        messageMapper.insertMessage(message);
    }
}
